﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.BusinessRule.System;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class GIS_ClearLatLng : PageBase
{

    private const string NEW_PAGE = "Customer_Maintain.aspx";
    private OrgainzationManager orgManager = new OrgainzationManager();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.txtHidOrg.Value == "")
        {
            this.txtHidOrg.Value = OrgHidValue;
            this.txtOrg.Text = OrgShowValue;

        }
        if (!IsPostBack)
        {
            DataBind();
            InitForm();
            
        }
    }

    #region 数据绑定
    private void InitForm()
    {

        //绑定客户渠道
        DictionaryItemsManager dim = new DictionaryItemsManager();
        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();
        ddlChannel.Items.Insert(0, new ListItem(Resources.language.ddl_All, ""));

    }
    #endregion


    #region 绑定查询数据
    public void BindGrid()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        if (txtHidOrg.Value != string.Empty)
        {
            sb.Append(string.Format(" AND C.ORG_ID IN ({0})", txtHidOrg.Value));

        }

        if (txtCustomer.Text != string.Empty)
        {
            sb.Append(" AND (C.[CODE] LIKE '%" + txtCustomer.Text.Trim() + "%' or C.[NAME] LIKE '%" + txtCustomer.Text.Trim() + "%') AND C.DOMAIN_ID='" + this.MyStateManager.DomainID + "' ");
        }

        if (!string.IsNullOrEmpty(txtCustomerID.Text.Trim()))
        {
            sb.Append(" And C.ID like '%" + txtCustomerID.Text.Trim() + "%' ");
        }
        if (ddlChannel.SelectedValue != string.Empty)
        {
            sb.Append(" AND LOCAL_LEVEL1_CODE =  '" + ddlChannel.SelectedValue + "'  ");
        }

        DataSet ds = CustomerManager.SearchCustomer(sb.ToString(), "0", "1", 0, 0, true);
        if (ds != null && ds.Tables.Count > 0)
        {
            gvChannelType.DataSource = ds;
            gvChannelType.DataBind();
        }


    }
    #endregion

    //查询
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    public void GridViewToolbarUserControl1_OnGVTBSetColumnsButtonEvent(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void gvChannelType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChannelType.PageIndex = e.NewPageIndex;
        BindGrid();
    }


    protected void btnNew_Click(object sender, EventArgs e)
    {
        var rowCount = gvChannelType.Rows.Count;
        CustomerManager customerMng = new CustomerManager();
        try
        {
            for (int i = 0; i < rowCount; i++)
            {
                CheckBox chk = (CheckBox)gvChannelType.Rows[i].FindControl("chkClear");


                if (chk != null && chk.Checked)
                {
                    decimal customerID = decimal.Parse(gvChannelType.Rows[i].Cells[0].Text.ToString());

                    CUSTOMERS customer = customerMng.Load(customerID);
                    customer.IDENTITY1 = 0;
                    customer.IDENTITY2 = 0;
                    customerMng.Save(customer);
                }
            }

            BindGrid();
        }
        catch (Exception ex)
        {
            ShowMessage("操作失败,异常信息:" + ex.Message);
        }
    }
}
